﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AppCash
{
    public partial class frmShop : Form
    {
        public frmShop()
        {
            InitializeComponent();
        }

        private void frmShop_Load(object sender, EventArgs e)
        {
            ZhiKeDuoServer.BLL.ShopInfo bShop = new ZhiKeDuoServer.BLL.ShopInfo();
            ZhiKeDuoServer.Model.ShopInfo mShop = new ZhiKeDuoServer.Model.ShopInfo();
            mShop = bShop.GetModel(1);
            tbShopName.Text = mShop.Name;
            tbPhone.Text = mShop.Phone;
            tbAddr.Text = mShop.Addr;
            txtshop.Text = mShop.Shop.ToString();
            try
            {
                string json = System.IO.File.ReadAllText(System.Environment.CurrentDirectory + "/serverconfig");
                var rlt = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
                txtaddr.Text = rlt["serveraddr"];
            }
            catch (Exception ex)
            {
                txtaddr.Text = "http://hat.ohthink.com/";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ZhiKeDuoServer.BLL.ShopInfo bShop = new ZhiKeDuoServer.BLL.ShopInfo();
            ZhiKeDuoServer.Model.ShopInfo mShop = new ZhiKeDuoServer.Model.ShopInfo();
            mShop.Id = 1;
            mShop.Name = tbShopName.Text;
            mShop.Addr = tbAddr.Text;
            mShop.Phone = tbPhone.Text;
            mShop.Shop = Convert.ToInt32(txtshop.Text);
            if (bShop.Update(mShop))
            {

                Dictionary<string, string> keys = new Dictionary<string, string>();
                string json = "";
                try
                {
                    json = System.IO.File.ReadAllText(System.Environment.CurrentDirectory + "/serverconfig");
                    keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
                }
                catch (Exception ex)
                {

                }
                if (keys.ContainsKey("serveraddr"))
                {
                    keys["serveraddr"] = txtaddr.Text;
                }
                else
                {

                    keys.Add("serveraddr", txtaddr.Text);
                }



                json = JsonConvert.SerializeObject(keys);
                System.IO.File.WriteAllText(System.Environment.CurrentDirectory + "/serverconfig", json);



                DevComponents.DotNetBar.MessageBoxEx.Show("修改成功!");
            }
            else
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("操作失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
